package person.learn.interceptors;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by barton on 16-1-27.
 * 1.拦截器拦截的是请求，而不是资源
 * 2.结合SpringBootLearnApplication的父类WebMvcConfigurerAdapter以及重写的addInterceptors()
 * 方法进行测试
 */
@Component
public class URLInterceptor implements HandlerInterceptor {

    /**
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object
            handler) throws Exception {
        System.out.println("在HandlerMapping确定了相应的 handler之后，在相应的handler后执行前执行该方法");

        // 返回true才能继续执行下一个interceptor
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object
            handler, ModelAndView modelAndView) throws Exception {
        System.out.println("在相应的 handler正确执行后，`");

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object
            handler, Exception ex) throws Exception {
        System.out.println("在request完全正确的处理完成，并且返回了相应的视图后执行该方法");
    }
}
